package cn.dyp.com.agriculture;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;

import cn.dyp.com.agriculture.request.BaseRequest.RequestResult;
import cn.dyp.com.agriculture.request.RequestThread;
import cn.dyp.com.agriculture.util.IpUtil;

/**
 * 程序启动的入口 提供全局的数据存储
 *username、password、ip
 * Created by lenovo on 2017/5/7.
 */

public class ClientApp extends Application{

    private SharedPreferences mSharedPreferences;//键值对的存储方式
    private String mServerIpStr = "";//保存用户输入的Ip
    private Handler mHandler;
    private AlertDialog mDialog;

    @Override
    public void onCreate() {
        super.onCreate();
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);//得到SharedPreferences对象
        handleMessage();
    }

    private void handleMessage() {

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) { //根据收到的消息的what类型处理
                    case RequestThread.MSG_REQUEST_RESULT:
                        RequestResult result = RequestResult.values()[msg.arg1];
                        RequestThread requestThread = (RequestThread) msg.obj;
                        if (requestThread != null) {
                            requestThread.hanlderResult(result);
                        }
                        if (result.equals(RequestResult.RESULT_NO_NET)) {
                            if (mDialog != null) {
                                if (!mDialog.isShowing()) {
                                    mDialog.setMessage(getString(R.string.no_net_work));
                                    mDialog.show();
                                }
                            }
                        }
                        if (result.equals(RequestResult.RESULT_FAIL)) {
                            if (mDialog != null) {
                                if (!mDialog.isShowing()) {
                                    mDialog.setMessage(getString(R.string.failed));
                                    mDialog.show();
                                }
                            }
                        }
                        break;
                }
                return false;
            }
        });
    }

    // 设置用户名
    public void setUsername(String username) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString("username", username);
        editor.commit();
    }
    // 获取用户名，return用户名
    public String getUsername() {
        return mSharedPreferences.getString("username", "");
    }

    // 设置密码
    public void setPassword(String password) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString("password", password);
        editor.commit();
    }
    // 获取密码，return密码
    public String getPassword() {
        return mSharedPreferences.getString("password", "");
    }

    // 保存用户输入的IP
    public void setServerIpStr(String serverIpStr) {
        this.mServerIpStr = serverIpStr;
        SharedPreferences.Editor editor = mSharedPreferences.edit();// 获取SharedPreferences对象
        editor.putString("IpStr", mServerIpStr);// 添加字符串
        editor.commit();// 数据提交，完成数据存储
    }
    // 判断用户输入IP地址，如果用户没有输入，则获取本地IP，return IP地址
    public String getServerIpStr() {
        mServerIpStr = mSharedPreferences.getString("IpStr", "");
        if (mServerIpStr.equals("")) {
            try {
                mServerIpStr = IpUtil.getLocalIpAddress(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mServerIpStr;
    }

    //获得Handler对象
    public Handler getHandler() {
        return mHandler;
    }

    //无网络或本次操作失败，显示对话框
    private void showAlertDialog(Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(getString(R.string.prompt));
        //builder.setMessage(getString(R.string.no_net_work));
        builder.setPositiveButton(activity.getString(android.R.string.ok), null);//点击确定按钮不关闭对话框
        mDialog = builder.create();//创建Dialog,不显示
    }

    //给Client设置Activity
    public void setActivity(Activity activity) {
        showAlertDialog(activity);
    }
}
